package com.ymatou.iapi.selleraccount.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.selleraccount.service.CreateSellerSubAccountCall;
import com.ymatou.iapi.selleraccount.service.DeleteSellerSubAccountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtSellerSubAccountRelated;
import com.ymttest.database.sqlwapper.YmtSellerSubAccountRelatedIMapper;

/**
 * @since 2016-12-25
 * @author chencan
 * 子账号创建接口
 *
 */
public class Tc_CreateSellerSubAccount {
	private static CreateSellerSubAccountCall createSellerSubAccountCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("业务说明：创建子账号");
	}

	@Before
	public void caseUp() {
		createSellerSubAccountCall = new CreateSellerSubAccountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createSellerSubAccountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateSellerSubAccount_001(){
		Logger.start(true, "创建子账号");
		try {
			
			int sellerId=3383;
			String subSellerName = "wesper_c021";
			String email = "can258456021@ymatou.com";
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",sellerId);
			map1.put("sellersubaccount",subSellerName);
			map1.put("email",email);
			
			JSONObject create = new CreateSellerSubAccountCall().callAndGetReturnData(map1);
			int retBea = create.getInt("Code");
			boolean success = create.getJSONObject("Data").getBoolean("Success");
			
			List<YmtSellerSubAccountRelated> account = new YmtSellerSubAccountRelatedIMapper().selectSubAccountRelatedsellersubaccount(subSellerName);
			YmtSellerSubAccountRelated first =account.get(0);
			int seller = first.getSellerid();
			int subsellerid = first.getSubsellerUserid();
			String Email = first.getEmail();
			System.out.println(subsellerid);
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("SellerId",seller);
			map.put("SellerSubAccountUserid",subsellerid);
			
			JSONObject deleteaccount = new DeleteSellerSubAccountCall().callAndGetReturnData(map);
			int retBeas = deleteaccount.getInt("Code");
			
			List<YmtSellerSubAccountRelated> accounts = new YmtSellerSubAccountRelatedIMapper().selectSubAccountRelatedsellersubaccount(subSellerName);
			YmtSellerSubAccountRelated firsts =accounts.get(0);
			int isdeleted = firsts.getIsdeleted();
			
			Logger.verifyEquals(200, retBea, "返回结果等于200");
			Logger.verifyEquals(true, success, "返回状态");
			Logger.verifyEquals(seller, sellerId, "主账号ID");
			Logger.verifyEquals(email, Email, "子账号邮箱");
			Logger.verifyEquals(1, isdeleted, "子账号状态");
			
			
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateSellerSubAccount_002(){
		Logger.start(true, "创建子账号--用户名已存在");
		try {
			
			int sellerId=3383;
			String sellersubaccount = "wesper_c012";
			String email = "can25845605@ymatou.com";
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",sellerId);
			map1.put("sellersubaccount",sellersubaccount);
			map1.put("email",email);
			
			JSONObject create = new CreateSellerSubAccountCall().callAndGetReturnData(map1);
			int retBea = create.getInt("Code");
			boolean success = create.getJSONObject("Data").getBoolean("Success");
			String message = create.getJSONObject("Date").getString("ErrorMessage");
			
			
			Logger.verifyEquals(200, retBea, "返回结果等于200");
			Logger.verifyEquals(false, success, "返回状态");
			Logger.verifyEquals("登录昵称已存在", message, "错误描述");
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateSellerSubAccount_003(){
		Logger.start(true, "创建子账号--邮箱已存在");
		try {

			int sellerId=3383;
			String email = "can25845605@ymatou.com";
			
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",sellerId);
			map1.put("email",email);
			
			JSONObject create = new CreateSellerSubAccountCall().callAndGetReturnData(map1);
			int retBea = create.getInt("Code");
			boolean success = create.getJSONObject("Data").getBoolean("Success");
			String message = create.getJSONObject("Data").getString("ErrorMessage");
			
			Logger.verifyEquals(200, retBea, "返回结果等于200");
			Logger.verifyEquals(false, success, "返回状态");
			Logger.verifyEquals("邮箱地址已存在", message, "错误描述");
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
